		TITLE	PASS2 - Copyright (C) 1994 SLR Systems

		INCLUDE	MACROS
		INCLUDE	SEGMENTS
		INCLUDE	GROUPS
		INCLUDE	SYMBOLS
		INCLUDE	EXES
if	fg_segm
		INCLUDE	SEGMSYMS
endif

		.DATA

		EXTERNDEF	SYMBOL_TABLE_USE:BYTE,MDB_TABLE_USE:BYTE

		EXTERNDEF	FLAG_0C:DWORD,FIRST_STACK_GINDEX:DWORD,FIRST_EXTERNAL_GINDEX:DWORD,CURNMOD_GINDEX:DWORD
		EXTERNDEF	FIXUPP_COUNT:DWORD,STACK_SIZE:DWORD,SEGM_OUT_TABLE:DWORD,EXE_OUT_TABLE:DWORD,PE_OUT_TABLE:DWORD
		EXTERNDEF	SEGMENT_TABLE:DWORD

		EXTERNDEF	EXEHEADER:EXE,NEXEHEADER:NEXE,SEGMENT_GARRAY:STD_PTR_S,GROUP_GARRAY:STD_PTR_S,XREF_OK_SEM:QWORD
		EXTERNDEF	SSYM_STUFF:ALLOCS_STRUCT,SYMBOL_GARRAY:STD_PTR_S,MDB_GARRAY:STD_PTR_S,PEXEHEADER:PEXE

		EXTERNDEF	OUT_FLUSH_SEGMOD:DWORD,OUT_INIT:DWORD


		.CODE	PASS2_TEXT

		externdef	_select_output:proc
		EXTERNDEF	COM_FLUSH_SEGMOD:PROC,STORE_VECTOR_TABLE_ADDRESS:PROC,FIXUPP2:PROC,DO_FARCALL_FIXUPPS:PROC
		EXTERNDEF	BUILD_FLUSH_ENTRIES:PROC,CONV_AX_SEGTBL_ESDI:PROC,RELEASE_DSBX:PROC,ALLOW_LINNUMS_MAP:PROC
		EXTERNDEF	ALLOW_XREF_MAP:PROC,ALLOW_SECTS_MAP:PROC,ALLOW_START_MAP:PROC,_release_minidata:proc
		EXTERNDEF	RELEASE_GARRAY:PROC,ERROR_UNDEFINED:PROC,RELEASE_PARALLEL_ARRAY:PROC,INIT_SET_RELOC_BITS:PROC
		EXTERNDEF	SAY_VERBOSE:PROC

		EXTERNDEF	OUT_TYPES:ABS




xSTORE_STACK_SEGMENT	PROC	NEAR
		;
		;
		;
if	fg_pe
		BITT	OUTPUT_PE
		JNZ	L3$
endif

if	fg_norm_exe
		BITT	OUTPUT_COM_SYS
		JNZ	L9$
endif
		MOV	ESI,FIRST_STACK_GINDEX

		TEST	ESI,ESI
		JZ	L9$
		;
		;OK, JUST PUT FRAME AND OFFSET IN EXE HEADER
		;
		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT

		MOV	EBX,[ESI]._SEG_LEN
		MOV	STACK_SIZE,EBX
if	fg_segm AND fg_norm_exe
		BITT	OUTPUT_SEGMENTED
		JZ	OUTPUT_NORMAL
endif
if	fg_segm
		TEST	FLAG_0C,MASK APPTYPE
		JNZ	L57$				;IGNORE STACK FOR LIBRARY
		TEST	[ESI]._SEG_TYPE,MASK SEG_IN_DGROUP
		JNZ	L5$
L59$:
		ADD	EBX,[ESI]._SEG_OFFSET
		MOV	EAX,[ESI]._SEG_OS2_NUMBER
L571$:
		XOR	EDX,EDX
		MOV	WPTR NEXEHEADER._NEXE_SSSP,BX	;RETAIN OFFSET IF NOT DGRUOP
;		MOV	NEXEHEADER._NEXE_SP_HW,CX	;OR NOT LAST SEGMENT IN DGROUP
		MOV	WPTR NEXEHEADER._NEXE_SSSP+2,AX
		MOV	STACK_SIZE,EDX			;07-01-93 FIX STACK+DGROUP TROUBLE...
L9$:
		RET

L57$:
		XOR	EAX,EAX
		XOR	EBX,EBX
		XOR	ECX,ECX
		JMP	L571$

L5$:
		;
		;MAKE SURE THIS IS LAST SEGMENT IN DGROUP
		;
		MOV	EDI,[ESI]._SEG_NEXT_SEG_GINDEX
		MOV	EAX,[ESI]._SEG_OS2_NUMBER

		TEST	EDI,EDI
		JZ	L55$		;LAST SEGMENT, OK

		CONVERT	EDI,EDI,SEGMENT_GARRAY
		ASSUME	EDI:PTR SEGMENT_STRUCT

		CMP	[EDI]._SEG_OS2_NUMBER,EAX
		JZ	L59$		;MORE SEGMENTS SAME SEGMENT #
L55$:
		;
		;OK, WE SHOW THIS AS 'ADDITIONAL STACK ALLOCATION' AND SUBTRACT ITS SIZE FROM DGROUP SEGMENT
		;
		MOV	ECX,EAX

		SHL	ECX,16

		MOV	NEXEHEADER._NEXE_STACKSIZE,BX		;ADDITIONAL STACK ALLOCATION
		MOV	NEXEHEADER._NEXE_SSSP,ECX		;SHOW DGROUP SEGMENT BUT OFFSET 0
;		MOV	STACK_SIZE,BX

		CONV_EAX_SEGTBL_ECX

		SUB	[ECX]._SEGTBL_LSIZE,EBX			;ADJUST DGROUP SEGMENT SIZE
		JNZ	L58$

		XOR	EAX,EAX
		ADD	[ECX]._SEGTBL_LSIZE,EBX			;OOPS, DGROUP IS ALL STACK, MUST SHOW IT AS SEGMENT SIZE...
		MOV	STACK_SIZE,EAX
		MOV	NEXEHEADER._NEXE_STACKSIZE,AX		;ADDITIONAL STACKSIZE == ZERO
		MOV	WPTR NEXEHEADER._NEXE_SSSP,BX
L58$:
		RET


if	fg_pe
L3$:
		TEST	FLAG_0C,MASK APPTYPE
		JZ	L31$
		;
		;IGNORE STACK FOR LIBRARY
		;
		XOR	EAX,EAX
		MOV	STACK_SIZE,EAX
		MOV	PEXEHEADER._PEXE_STACK_COMMIT,EAX
L31$:
		MOV	EAX,STACK_SIZE
		MOV	PEXEHEADER._PEXE_STACK_RESERVE,EAX
		RET
endif


OUTPUT_NORMAL	LABEL	PROC

endif
if	fg_norm_exe

		ADD	EBX,[ESI]._SEG_OFFSET
		MOV	EAX,[ESI]._SEG_FRAME

		BITT	STACK_GROUP_FLAG		;RECOGNIZE GROUP?
		JZ	DSS_1

		CMP	[ESI]._SEG_GROUP_GINDEX,0	;IN A GROUP?
		JZ	DSS_1

		MOV	ESI,[ESI]._SEG_GROUP_GINDEX
		CONVERT	ESI,ESI,GROUP_GARRAY
		ASSUME	ESI:PTR GROUP_STRUCT

		MOV	EAX,[ESI]._G_FRAME

DSS_1		LABEL	PROC

		;
		;OK, STORE OFFSET
		;
		SUB	EBX,EAX

		MOV	EXEHEADER._EXE_REG_SP,BX

		SHR	EAX,4

		MOV	EXEHEADER._EXE_REG_SS,AX
endif
		RET

xSTORE_STACK_SEGMENT	ENDP


SELECT_OUTPUT	PROC	NEAR
	push	EAX
	call	_select_output
	add	ESP,4
	ret
SELECT_OUTPUT	ENDP


if	debug

		.CONST

SYM_REL_MSG	DB	SIZEOF SYM_REL_MSG-1,'Releasing Symbols',0dh,0Ah

endif

		END

